package com.rm.zeyou.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.constant.NumberConstants;
import com.rm.zeyou.entity.LimitCouponMatchesConfig;
import com.rm.zeyou.mapper.LimitCouponMatchesConfigMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.LimitCouponMatchesConfigService;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.util.date.DateUtils;
import com.rm.zeyou.vo.response.SelectLimitCouponMatchesListResponseVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 限时优惠券场次表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:08
 */
@Service
public class LimitCouponMatchesConfigServiceImpl extends ServiceImpl<LimitCouponMatchesConfigMapper, LimitCouponMatchesConfig> implements LimitCouponMatchesConfigService {
    @Resource
    private LimitCouponMatchesConfigMapper limitCouponMatchesConfigMapper;


    @Override
    public ApiRestResponse<List<SelectLimitCouponMatchesListResponseVo>> selectLimitCouponMatchesList(Long userId) {
        List<LimitCouponMatchesConfig> limitCouponMatchesConfigs = limitCouponMatchesConfigMapper.selectList(new LambdaQueryWrapper<LimitCouponMatchesConfig>()
                .orderByDesc(LimitCouponMatchesConfig::getSnappedTime));
        List<SelectLimitCouponMatchesListResponseVo> voList = new ArrayList<>();
        if (!CheckEmptyUtil.isEmpty(limitCouponMatchesConfigs)) {
            limitCouponMatchesConfigs.forEach(item -> {
                SelectLimitCouponMatchesListResponseVo vo = new SelectLimitCouponMatchesListResponseVo();
                vo.setId(item.getId());
                vo.setLimitStatus(item.getLimitStatus());
                if (item.getLimitStatus().equals(NumberConstants.ZERO_CONSTANTS)) {
                    //计算开场倒计时
                    vo.setCountdownMilliseconds(DateUtil.between(new Date(), item.getSnappedTime(), DateUnit.SECOND,false));
                }
                vo.setOpenWeek(DateUtils.getWeek(item.getSnappedTime()));
                vo.setOpenTime(new SimpleDateFormat("HH:mm").format(item.getSnappedTime()));
                voList.add(vo);

            });
        }

        return ApiRestResponse.success(voList);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void openLimitCoupon() {
        List<LimitCouponMatchesConfig> limitCoupons = limitCouponMatchesConfigMapper.selectList(new LambdaQueryWrapper<LimitCouponMatchesConfig>()
                .eq(LimitCouponMatchesConfig::getLimitStatus, NumberConstants.ZERO_CONSTANTS));
        if (! CheckEmptyUtil.isEmpty(limitCoupons)) {
            for (LimitCouponMatchesConfig limitCoupon : limitCoupons) {
                if (new Date().after(limitCoupon.getSnappedTime())) {
                    limitCoupon.setLimitStatus(NumberConstants.ONE_CONSTANTS);
                    limitCouponMatchesConfigMapper.updateById(limitCoupon);
                }
            }
        }
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void closeLimitCoupon() {
        List<LimitCouponMatchesConfig> limitCoupons = limitCouponMatchesConfigMapper.selectList(new LambdaQueryWrapper<LimitCouponMatchesConfig>()
                .eq(LimitCouponMatchesConfig::getLimitStatus, NumberConstants.ONE_CONSTANTS));
        if (! CheckEmptyUtil.isEmpty(limitCoupons)) {
            for (LimitCouponMatchesConfig limitCoupon : limitCoupons) {
                if (new Date().after(DateUtil.offsetMinute(limitCoupon.getSnappedTime(),limitCoupon.getLimitCouponExpiredTime()))) {
                    limitCoupon.setLimitStatus(NumberConstants.TWO_CONSTANTS);
                    limitCouponMatchesConfigMapper.updateById(limitCoupon);
                }
            }
        }
    }
}
